package com.im.foodorderingsystem;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.im.foodorderingsystem.db.FOSDB;
import com.im.foodorderingsystem.task.LoginTask;

public class Login extends ActionBarActivity {

	EditText etUsername;
	EditText etPassword;
	
	SharedPreferences fosSettings;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		fosSettings=getSharedPreferences("fosSettings", Context.MODE_PRIVATE);
		
		etUsername = (EditText) findViewById(R.id.etUsername);
		etPassword = (EditText) findViewById(R.id.etPassword);
		
		System.out.println("login activity");

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void login(View v) {
		String username = etUsername.getText().toString();
		String passwordd = etPassword.getText().toString();

		if (username.equals("")) {
			etUsername.setError("Please Fill");
		}else if (passwordd.equals("")) {
			etPassword.setError("Please Fill");
		}else{
			String [] parameters;
			parameters=new String[2];
			parameters[0]=username;
			parameters[1]=passwordd;
			
			LoginTask lt = new LoginTask(this);
			lt.execute(parameters);
		}

	}

	
	/*
	private class LoginTask extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... parameters) {
			System.out.println(parameters[0]+ " " +parameters[1]);

			
//http://10.0.3.2 for genymortion
//http://10.0.2.2 for normal emulater			
			
			//String URL = "http://10.0.2.2/androidtest/index.php?r=contact/createm";
			String URL = "http://10.0.3.2/foodorderingsystem/index.php?r=api/user/login&email="+parameters[0]+"&password="+parameters[1];
			//String URL = Utility.host+"/foodorderingsystem/index.php?r=api/user/login&email="+parameters[0]+"&password="+parameters[1];
			DefaultHttpClient httpclient = new DefaultHttpClient(
					new BasicHttpParams());
			HttpGet httppost = new HttpGet(URL);
			httppost.setHeader("Content-type", "application/json");
			InputStream inputStream = null;
			String result = null;

			try {
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
				inputStream = entity.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(inputStream, "UTF-8"), 8);
				StringBuilder theStringBuilder = new StringBuilder();
				String line = null;
				while ((line = reader.readLine()) != null) {
					theStringBuilder.append(line + "\n");
				}
				result = theStringBuilder.toString();

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (inputStream != null)
						inputStream.close();
				} catch (Exception e) {
				}
			}

			return result;
		}

		@Override
		protected void onPostExecute(String result) {

			System.out.println(result);
			
//			Editor editor=settings.edit();
//			editor.putString("username", username);
//			editor.putString("email", email);
//			
//			editor.commit();

			try {
				JSONObject reader = new JSONObject(result);
				String response=reader.getString("response");
				
				JSONObject sys  = reader.getJSONObject("user");
				String  user_id= sys.getString("user_id");
				String  email= sys.getString("email");
				
				//System.out.println("user_id="+user_id+" email="+email);
				
				Editor editor=fosSettings.edit();
				editor.putString("user_id", user_id);
				editor.putString("email", email);
				
				editor.commit();
				
				
				if(response.equals("true")){
					Intent intent = new Intent(getApplicationContext(), Home.class);
					startActivity(intent);
				}else{
					Toast.makeText(getApplicationContext(), "Incorect username or password", Toast.LENGTH_LONG).show();
				}


			} catch (Exception e) {
				// TODO: handle exception
			}

			// super.onPostExecute(result);
		}

	}

	*/
}
